package utilities.manager.definederunner;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.util.Properties;

import utilities.manager.Runner;
import utilities.module.Module;

public class GameRunner extends Runner 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8448874543364976009L;
	
	protected Module mainModule;
	
	/*******************************************************************
	 * 
	 * CONSTRUCTOR
	 * 
	 *******************************************************************/

	/**
	 * 
	 * @param width
	 * @param height
	 * @param backgroundColor
	 */
	public GameRunner(Container parent, Module mainModule)
	{
		super(parent.getWidth(), parent.getHeight(), Color.black);
		this.mainModule = mainModule;
		
		start();
		parent.add(this);
		parent.addMouseListener(this);
		parent.addMouseMotionListener(this);
		parent.addMouseWheelListener(this);
		parent.addKeyListener(this);
	}
	
	/*******************************************************************
	 * 
	 * INHERITED METHODS
	 * 
	 *******************************************************************/

	/**
	 * 
	 */
	public void gameUpdate(Properties globals) 
	{
		if(mainModule != null) mainModule.update(globals);
	}

	/**
	 * 
	 */
	public void drawScreen(Graphics g)
	{
		if(mainModule != null) mainModule.drawMe(g);
	}

	/**
	 * 
	 */
	public void keyTyped(KeyEvent e) 
	{
		// Call focus module with key event information
		if(mainModule != null) mainModule.keyTyped(e);
	}
	
	/**
	 * 
	 */
	public void keyPressed(KeyEvent e)
	{
		// Call focus module with key event information
		if(mainModule != null) mainModule.keyPressed(e);
	}

	/**
	 * 
	 */
	public void keyReleased(KeyEvent e)
	{
		// Call focus module with key event information
		if(mainModule != null) mainModule.keyReleased(e);
	}
	
	/**
	 * 
	 */
	public void mouseClicked(MouseEvent e)
	{
		// Get mouse position within content area
		setMouseXY(e);
		
		// Call focus module with mouse event information
		if(mainModule != null) mainModule.mouseClicked(e);
	}

	public void mousePressed(MouseEvent e)
	{
		// Get mouse position within content area
		setMouseXY(e);
		
		// Call focus module with mouse event information
		if(mainModule != null) mainModule.mousePressed(e);
	}

	public void mouseReleased(MouseEvent e)
	{
		// Get mouse position within content area
		setMouseXY(e);
		
		// Call focus module with mouse event information
		if(mainModule != null) mainModule.mouseReleased(e);
	}

	public void mouseEntered(MouseEvent e)
	{
		// Get mouse position within content area
		setMouseXY(e);
		
		// Call focus module with mouse event information
		if(mainModule != null) mainModule.mouseEntered(e);
	}

	public void mouseExited(MouseEvent e)
	{
		// Get mouse position within content area
		setMouseXY(e);
		
		// Call focus module with mouse event information
		if(mainModule != null) mainModule.mouseExited(e);
	}

	public void mouseDragged(MouseEvent e)
	{
		// Get mouse position within content area
		setMouseXY(e);
		
		// Call focus module with mouse event information
		if(mainModule != null) mainModule.mouseDragged(e);
	}

	public void mouseMoved(MouseEvent e)
	{
		// Get mouse position within content area
		setMouseXY(e);
		
		// Call focus module with mouse event information
		if(mainModule != null) mainModule.mouseMoved(e);
	}

	public void mouseWheelMoved(MouseWheelEvent e)
	{
		// Get mouse position within content area
		setMouseXY(e);
		
		// Call focus module with mouse event information
		if(mainModule != null) mainModule.mouseWheelMoved(e);
	}
	
	/*******************************************************************
	 * 
	 * METHODS
	 * 
	 *******************************************************************/
	
	/**
	 * 
	 * @param mod
	 * @return
	 */
	public Module setMainModule(Module mod)
	{
		Module old = this.mainModule;
		this.mainModule = mod;
		return old;
	}
	
	/**
	 * 
	 * @return
	 */
	public Module getMainModule()
	{
		return mainModule;
	}
}